package org.example.service;

import com.google.common.collect.Lists;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.example.dao.SkuRepository;
import org.example.entity.Sku;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SkuService {

    @Resource
    private SkuRepository skuRepository;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    public void searchByCategoryId() {
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery("categoryId", 101L))
                // date, int, bigDecimal都可以直接指明字段排序
                .withSort(SortBuilders.fieldSort("shelfTime").order(SortOrder.DESC))
                .build();
        List<SearchHit<Sku>> searchHits = elasticsearchRestTemplate.search(query, Sku.class).getSearchHits();
    }

    public void inset() {
        Sku sku = new Sku();
        sku.setCategoryId(101);
        sku.setPrice(new BigDecimal("99.99"));
        sku.setShelfTime(new Date());

        // 动态构建 attributeMap
        Map<String, Object> attributeMap = new HashMap<>();
        attributeMap.put("王者荣耀-按系统", "苹果微信");
        attributeMap.put("王者荣耀-实名情况", "不可二次实名");
        attributeMap.put("王者荣耀-有无防沉迷", "无防沉迷");
        attributeMap.put("王者荣耀-信誉分是否可排位", "可排位");
        attributeMap.put("王者荣耀-贵族等级", "无贵族等级");
        attributeMap.put("王者荣耀-天幕", Lists.newArrayList("灿若星河天幕", "荣耀贵族天幕", "冠军登峰天幕", "集结狂飙天幕", "怪盗基德天幕", "敬我三分天幕"));
        attributeMap.put("王者荣耀-段位", Lists.newArrayList("传奇王者", "荣耀王者", "绝世王者", "非凡王者", "无双王者", "至圣王者"));
        attributeMap.put("王者荣耀-小国标", Lists.newArrayList("国服少司缘", "国服元流之子(法师)", "国服大司命", "国服莱西奥", "国服云缨", "国服金蝉"));
        attributeMap.put("王者荣耀-大国标", Lists.newArrayList("大国标艾琳", "大国标镜", "大国标鲁班大师", "大国标上官婉儿", "大国标女娲", "大国标钟馗"));
        attributeMap.put("王者荣耀-传说皮肤", Lists.newArrayList("鲁班七号时之奇旅", "花木兰冠军飞将", "李元芳飞鸢探春", "女娲补天", "伽罗太华", "露娜霜月吟"));

        attributeMap.put("王者荣耀-史诗皮肤数量", 1);
        attributeMap.put("王者荣耀-皮肤数量", 3);
        attributeMap.put("王者荣耀-传说皮肤数量", 2);

        sku.setAttribute(attributeMap);
        Sku save = skuRepository.save(sku);

        // 动态索引
//        elasticsearchRestTemplate.save(sku, IndexCoordinates.of("sku_" + sku.getCategoryId()));
        sku.setId(save.getId());
    }

    public void inset2() {
        Sku sku = new Sku();
        sku.setCategoryId(102);
        sku.setPrice(new BigDecimal("99.99"));
        sku.setShelfTime(new Date());

        // 动态构建 attributeMap
        Map<String, Object> attributeMap = new HashMap<>();
        attributeMap.put("无畏契约-账号类型", "微信");
        attributeMap.put("无畏契约-实名情况", "可二次实名");
        attributeMap.put("无畏契约-段位", "源能战魂");
        attributeMap.put("无畏契约-2021全球冠军赛套装", Lists.newArrayList("近战武器-2021全球冠军赛爪刀"));
        attributeMap.put("无畏契约-近战武器", Lists.newArrayList("近战武器-2023全球冠军赛苦无", "近战武器-2024全球冠军赛战刀", "近战武器-2021全球冠军赛爪刀", "近战武器-显卡3.0爪刀", "近战武器-光圈记忆弹簧刀", "近战武器-闪耀之星荧光棒"));
        attributeMap.put("无畏契约-鬼魅", Lists.newArrayList("鬼魅-艾沃莉的梦之翼"));
        attributeMap.put("无畏契约-蜂刺", Lists.newArrayList("蜂刺-混沌序曲"));
        attributeMap.put("无畏契约-奥丁", Lists.newArrayList("奥丁-艾沃莉的梦之翼"));

        attributeMap.put("无畏契约-枪皮数量", 1);
        attributeMap.put("无畏契约-刀皮数量", 3);

        sku.setAttribute(attributeMap);
        Sku save = skuRepository.save(sku);
        sku.setId(save.getId());
    }
}
